import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class StringBufferDemoTest {
    StringBuffer str12 = new StringBuffer("hereare12chr");
    StringBuffer str24 = new StringBuffer("Here are 24 characters!!");
    StringBuffer str36 = new StringBuffer("Surprise!!! Here are 36 characters!!");

    @Test
    public void testcharAt(){
        Assert.assertEquals('h', str12.charAt(0));
        Assert.assertEquals('a', str24.charAt(5));
    }

    @Rule
    public ExpectedException thrown= ExpectedException.none();
    @Test
    public void testcharAtException(){
        thrown.expect(StringIndexOutOfBoundsException.class);
        str36.charAt(40);
    }

    @Test
    public void testlength(){
        Assert.assertEquals(12, str12.length());
        Assert.assertEquals(24, str24.length());
        Assert.assertEquals(36, str36.length());
    }

    @Test
    public void testcapacity(){
        Assert.assertEquals(28, str12.capacity());
        Assert.assertEquals(40, str24.capacity());
        Assert.assertEquals(52, str36.capacity());
        Assert.assertEquals(82,str24.append("here are 22 characters").capacity());
    }

    @Test
    public void testindex(){
        Assert.assertEquals(4, str12.indexOf("a"));
        Assert.assertEquals(9, str24.indexOf("24"));
        Assert.assertEquals(-1, str36.indexOf("?"));
    }
}